module("ufe.policies.random", package.seeall)
local m = require("ufe.ufe_module")

local action = "random"

local function check(acf)
    if type(acf.rate) ~= 'number' then
        return false, "rate in random must be a number"
    end

    if acf.rate < 0 or acf.rate > 1 then
        return false, "rate in random must be in 0 and 1"
    end

    return true
end

local function execute(acf)
    local r = math.random()
    --ngx.log(ngx.DEBUG,"r:"..tostring(r)," rate:"..tostring(acf.rate))
    if r <= acf.rate then
        return true
    end
    return false
end

math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,6)))
m.register_policy(action, {check = check, execute = execute })
